﻿namespace EasyWeb
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using EasyWeb.Data;
    using EasyWeb.Design;

    [EWDisplayName(Name = "FieldDefinitionWho", ResourceType = typeof(EWFieldDefinitionResources))]
    public class EWFieldDefinitionWho : EWFieldDefinition
    {
        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowDefaultValue")]
        public override bool AllowDefaultValue
        {
            get { return false; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowUnique")]
        public override bool AllowUnique
        {
            get { return false; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_DefaultValue")]
        public override object DefaultValue
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionWho_AllowMultiple")]
        public bool AllowMultiple
        {
            get { return this.StateBag.Get<bool>("AllowMultiple"); }
            set { this.StateBag.Set("AllowMultiple", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionWho_SelectionMode")]
        public EWWhoSelectionMode SelectionMode
        {
            get { return this.StateBag.Get<EWWhoSelectionMode>("SelectionMode", () => EWWhoSelectionMode.User); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("SelectionMode", value);
            }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionWho_SelectionSource")]
        public EWWhoSelectionSource SelectionSource
        {
            get { return this.StateBag.Get<EWWhoSelectionSource>("SelectionSource", () => EWWhoSelectionSource.Source); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("SelectionSource", value);
            }
        }

        public override EWFieldDefinitionColumnCollection CreateFieldDefinitionColumns()
        {
            return new EWFieldDefinitionColumnCollection()
            {
                new EWFieldDefinitionColumn()
                {
                    Name = this.InternalName,
                    IsNullable = this.AllowNull,
                    IsUnique = this.AllowUnique && this.IsUnique,
                    Type = DbType.Guid
                }
            };
        }

        public override void Deserialize(EWSerializationInfo info)
        {
            base.Deserialize(info);

            this.AllowMultiple = info.GetBoolean("AllowMultiple");
            this.SelectionMode = info.GetEnum<EWWhoSelectionMode>("SelectionMode");
            this.SelectionSource = info.GetEnum<EWWhoSelectionSource>("SelectionSource");
        }

        public override void Serialize(EWSerializationInfo info)
        {
            base.Serialize(info);

            info.AddValue("AllowMultiple", this.AllowMultiple);
            info.AddValue("SelectionMode", this.SelectionMode);
            info.AddValue("SelectionSource", this.SelectionSource);
        }
    }
}
